/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.add.ctrl;

import com.arg.deploy.bean.Tag;
import com.arg.deploy.bean.TagLookupCatalog;
import com.arg.deploy.bean.User;
import com.arg.deploy.core.hibernate.TagCore;
import com.arg.deploy.core.hibernate.TagLookupCatalogCore;
import com.arg.deploy.core.hibernate.UserCore;
import com.arg.deploy.webapp.common.AlertMessages;
import com.arg.deploy.webapp.common.MainPageController;
import com.arg.deploy.webapp.common.ResourceUtil;
import com.arg.deploy.webapp.common.ValidateManager;
import com.arg.deploy.webapp.common.ValidateUtil;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Textbox;

/**
 *
 * @author sorasaks
 */
public class AddTagReleaseCtrl extends MainPageController {

    public static final String TAG_RELEASE = "RELEASE";
    private static final Logger LOG = Logger.getLogger(AddTagReleaseCtrl.class);
    private Label lbTotal;
    private Textbox txtSearch;
    private Button btnSearch;
    private Button btnAdd;
    private Button btnDel;
    private Button btnSave;
    private Grid gridTag;
    private AddTagReleaseRenderer renderer;
    private Checkbox chkAll;
    private List<Tag> searchObject;
    private TagLookupCatalog catalog;

    public AddTagReleaseCtrl() {
        super("default.message.title.addtag.release", Boolean.TRUE);
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initialComponent();
        initialRenderer();
        initialEventListener();
    }

    private void initialComponent() {
        catalog = TagLookupCatalogCore.getLookupByName(TAG_RELEASE);

        if (catalog == null) {
            AlertMessages.alertMessage("ผิดพลาด ::: Tag Type ไม่ถูกต้องกรุณาตรวจสอบ");
            throw new NullPointerException();
        }
    }

    private void initialEventListener() {
        btnSave.addEventListener(Events.ON_CLICK, new SaveTagRendererEventListener());
        btnAdd.addEventListener(Events.ON_CLICK, new AddTagRendererEventListener());
        btnDel.addEventListener(Events.ON_CLICK, new DelTagRendererEventListener());
        btnSearch.addEventListener(Events.ON_CLICK, new SearchTagRendererEventListener());
        txtSearch.addEventListener(Events.ON_OK, new SearchTagRendererEventListener());
    }

    private void initialRenderer() {
        renderer = new AddTagReleaseRenderer(chkAll, gridTag);
        gridTag.setRowRenderer(renderer);
        doSearch(null);
    }

    private class SaveTagRendererEventListener implements EventListener {

        public SaveTagRendererEventListener() {
        }

        @Override
        public void onEvent(Event t) throws Exception {
            if (!validateSave()) {
                return;
            }

            ListModelList listM = (ListModelList) gridTag.getModel();
            List<Object> testTemplateList = listM.getInnerList();
            for (Object o : testTemplateList) {
                Tag tag = (Tag) o;

                TagLookupCatalog lookup = TagLookupCatalogCore.getLookupByName(TAG_RELEASE);
                User user = UserCore.getUserByUsername(tag.getUsername());
                tag.setTag(lookup);
                tag.setUser(user);

                LOG.info(":::::::::::::::::::::::: user = " + user);

                String filename = tag.getPath().split("/")[tag.getPath().split("/").length - 1];
                tag.setFilename(filename.trim());
                tag.setPath(tag.getPath().trim());
                tag.setFilename(tag.getFilename().trim());
                tag.setIssue(tag.getIssue().trim());
                tag.setRevision(tag.getRevision().trim());
                tag.setUsername(tag.getUsername().trim());
                tag.setCreateDate(new Date());

                LOG.info(":::::::::::::::::::::::: tag = " + tag);
                Boolean isSave = TagCore.save(tag, userByLogin);
                if (!isSave) {
                    AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.failed.save", locale));
                    return;
                }
            }

            AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.save", locale));

            doSearch(null);
        }
    }

    private class AddTagRendererEventListener implements EventListener {

        public AddTagRendererEventListener() {
        }

        @Override
        public void onEvent(Event t) throws Exception {
            if (!validateSave()) {
                return;
            }

            ListModelList listM = (ListModelList) gridTag.getListModel();
            List<Tag> list = listM.getInnerList();

            if (list == null) {
                list = new ArrayList<Tag>();
            }
            Tag tag = new Tag();
            tag.setMode("MODIFY");
            tag.setUser(userByLogin);
            list.add(0, tag);

            doSearch(list);
        }
    }

    private class DelTagRendererEventListener implements EventListener {

        public DelTagRendererEventListener() {
        }

        @Override
        public void onEvent(Event t) throws Exception {

            if (renderer.getCheckedSizeOfCheckbox() <= 0) {
                AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.validate", locale));
                return;
            }

//            if(AlertMessages.NO == AlertMessages.confirmMessage("คุณต้องการลบข้อมูลใช่หรือไหม่?")) {
//                return;
//            }
            AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.delete", locale));

            List<Tag> selectListValue = (List) renderer.selectTemplateList;
            List<Checkbox> selectListCheckbox = (List) renderer.selectTemplateCheckboxList;

            Tag tagClound = null;

            Boolean isBreak = Boolean.FALSE;
            for (Tag tag : selectListValue) {
                LOG.info(":::::::::::::::::::::::: tag = " + tag);
                Boolean isRemove = Boolean.FALSE;
                if (tag != null
                        && tag.getId() != null) {
                    isRemove = Boolean.TRUE;
                }
                if (isRemove) {
                    Boolean isSuccess = TagCore.remove(tag);
                    if (!isSuccess) {
                        tagClound = tag;
                        isBreak = Boolean.TRUE;
                        break;
                    }
                }
                Checkbox chkbox = selectListCheckbox.get(selectListValue.indexOf(tag));
                renderer.removeRow(chkbox);
            }

            if (isBreak && tagClound != null) {
                AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.failed.delete", locale));
            }

            AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.delete.success", locale));

            doSearch(null);

        }
    }

    private class SearchTagRendererEventListener implements EventListener {

        public SearchTagRendererEventListener() {
        }

        @Override
        public void onEvent(Event t) throws Exception {
            List<Tag> listTag = TagCore.getTag(txtSearch.getValue(), userByLogin, catalog, null, null);
            LOG.info("::::::::::::::::::::: listTag.size() = " + listTag.size());

            if (listTag.isEmpty()) {
                AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.no.data", locale));
            }

            doSearch(listTag);
        }
    }

    private Boolean validateSave() throws UnsupportedEncodingException {
        List<Row> listRow = renderer.getRowRenderer();
        HashMap paramValue = renderer.getRowComponent();

        LOG.info(":::::::::::::::::::::::::: listRow.size() = " + listRow.size());

        ValidateManager vm = new ValidateManager();
        for (Row row : listRow) {
            List<Component> listComp = (List<Component>) paramValue.get(row);
            Combobox txtMode = (Combobox) listComp.get(2);
            Textbox txtIssue = (Textbox) listComp.get(3);
            Textbox txtRevision = (Textbox) listComp.get(4);
            Textbox txtPath = (Textbox) listComp.get(5);
            Textbox txtAuthor = (Textbox) listComp.get(6);

            vm.add(ValidateUtil.requireField(txtMode));
            vm.add(ValidateUtil.requireField(txtIssue));
            vm.add(ValidateUtil.requireField(txtRevision));
            vm.add(ValidateUtil.requireField(txtPath));
            vm.add(ValidateUtil.requireField(txtAuthor));

            if (!vm.getValue()) {
                AlertMessages.alertMessage(vm.getMessageTemplate());
                return Boolean.FALSE;
            }

            String[] reversion = txtRevision.getValue().replace(".", ",").split(",");
            if (!(reversion.length == 2
                    || reversion.length == 4)) {
                vm.add(ValidateUtil.addError(txtRevision, ResourceUtil.getLabel("default.alert.message.invalid.revision", locale)));
                AlertMessages.alertMessage(vm.getMessageTemplate());
                return Boolean.FALSE;
            }

            if (txtPath.getValue().split("/").length <= 1) {
                vm.add(ValidateUtil.addError(txtPath, ResourceUtil.getLabel("default.alert.message.invalid.path", locale)));
                AlertMessages.alertMessage(vm.getMessageTemplate());
                return Boolean.FALSE;
            }

            User user = UserCore.getUserByUsername(txtAuthor.getValue());
            if (user == null) {
                vm.add(ValidateUtil.addError(txtAuthor, ResourceUtil.getLabel("default.alert.message.invalid.author", locale)));
                AlertMessages.alertMessage(vm.getMessageTemplate());
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    private void doSearch(List list) {
        txtSearch.setRawValue(null);
        if (list == null) {
            if (userByLogin != null) {
                searchObject = TagCore.getTag(null, userByLogin, catalog, null, null);
                lbTotal.setValue(String.valueOf(searchObject.size()));
            }
            renderer = new AddTagReleaseRenderer(chkAll, gridTag);
            generateRenderer(gridTag, renderer, searchObject);
        } else {
            lbTotal.setValue(String.valueOf(list.size()));
            renderer = new AddTagReleaseRenderer(chkAll, gridTag);
            generateRenderer(gridTag, renderer, list);
        }
    }

    private void generateRenderer(Grid grid, RowRenderer renderer, List<Tag> model) {
        grid.setRowRenderer(renderer);
        if (model != null) {
            grid.setModel(new ListModelList<Tag>(model));
        }
    }
}
